# -*- encoding: utf-8 -*-
# 
# Lúcuma
# Copyright © 2008 Diego Escalante Urrelo  <diegoe@gnome.org>
# 
# Licensed under the GNU General Public License Version 2
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
# 

from lucuma.settings import MEDIA_URL
from django.shortcuts import render_to_response
from lucuma.notes.models import Note
from lucuma.personas.models import Persona

def main(request):
    """
    Show all available Notes.
    """
    notes = Note.objects.all().order_by('-published')
    return render_to_response('notes_list.html', 
        {   'media_url': MEDIA_URL,
            'notes': notes,
            'tasks_tags_checkbox': False,
            'tasks_tags_preview': False,
            'tasks_tags_reply': False,
            'session' : request.session })

def single(request, note_id):
    """
    Show a single Note, identified by note_id.
    """
    #FIXME: plz, be safer
    note = Note.objects.get(pk=note_id)
    personas = Persona.objects.all()
    
    return render_to_response('notes_single.html', 
        {   'media_url': MEDIA_URL,
            'note': note,
            'personas': personas,
            'tasks_tags_preview': False,
            'tasks_tags_reply': False,
            'session' : request.session })

def tags_filter(request, tag_name):
    """
    Filter Notes by tags.
    """
    from lucuma.tags.models import Tag
    
    tag = Tag.objects.filter(name__exact=tag_name)
    if len(tag) == 1:
        notes = tag[0].note_set.all()
        return render_to_response('notes_tags_list.html',
            {   'media_url': MEDIA_URL,
                'notes': notes,
                'tag_name': tag_name,
                'session' : request.session })
    else:
        return main(request)

def add(request):
    """
    Renders the add Note form.
    """
    personas = Persona.objects.all()
    notes = Note.objects.all()
    return render_to_response('notes_add.html',
		{	'media_url': MEDIA_URL,
		    'personas': personas, 
		    'notes': notes,
			'session' : request.session })
